import jQuery from 'jquery'
	import {
		login,
		sendCodeToEmail,
		register
	} from '@/api/user.js'
	import {
		getPreView
	} from "@/cache/router.js"
	import {enter} from "@/utils/keyupUtil.js"
	import {addJs} from "@/utils/index.js"
	export default {
		name: "login",
		data() {
			var checkEmail = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('请输入邮箱'));
				}
				var numReg = "^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$"
				var numRe = new RegExp(numReg);
				if (!numRe.test(value)) {
					this.regist.email = "";
					return callback(new Error('请正确输入邮箱'));
				}
				callback();
			}
			var checkCode = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('请输入验证码'));
				}
				var numReg = /^[0-9]*$/
				var numRe = new RegExp(numReg);
				if (!numRe.test(value)) {
					this.regist.code = "";
					return callback(new Error('请正确输入验证码'));
				}
				callback();
			}
			return {
				user: {
					username: "",
					password: ""
				},
				regist: {
					email: "",
					username: "",
					password: "",
					showSendYZM: true,
					time: 180,
					code: ""
				},
				users: {
					username: [{
							required: true,
							message: '请输入用户名',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 12,
							message: '长度在 3 到 12 个字符',
							trigger: 'blur'
						}
					],
					password: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 18,
							message: '长度在 6 到 18 个字符',
							trigger: 'blur'
						}
					],
				},
				regists: {
					username: [{
							required: true,
							message: '请输入用户名',
							trigger: 'blur'
						},
						{
							min: 3,
							max: 12,
							message: '长度在 3 到 12 个字符',
							trigger: 'blur'
						}
					],
					password: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 18,
							message: '长度在 6 到 18 个字符',
							trigger: 'blur'
						}
					],
					email: [{
						validator: checkEmail,
						trigger: 'blur'
					}],
					code: [{
						validator: checkCode,
						trigger: 'blur'
					}]
				}
			}
		},
		computed: {
			userInfo() {
				return this.$store.state.userInfo;
			}
		},
		watch:{
		},
		methods: {
			qqLogin() {
				QC.Login.showPopup({
					appId: "101928598",
					redirectURI: "https://www.wayblogs.com/qq_redirect"
				});
				window.close();
			},
			//登录
			submitLogin() {
				this.$refs["user"].validate((valid) => {
					if (valid) {
						login(this.user.username, this.user.password).then(res => {
							this.$user.setUserInfo(res.data);
							this.$store.commit("setUserInfo", res.data)
							var _this = this;
							this.$message({
								message: '登录成功',
								type: 'success',
								duration: 1000,
								onClose() {
									var path = getPreView();
									//_this.$router.push(path ? path : '/');
									window.location.href = path ? path : '/';
									console.log(path)
								}
							});
						})
					}
				});
			},
			//发送验证码
			sendCodeToEmail() {
				sendCodeToEmail(this.regist.email).then(res => {
					this.regist.showSendYZM = false;
					var that = this;
					var timeer = setInterval(function() {
						that.regist.time--;
						if (that.regist.time == 0) {
							clearInterval(timeer);
							that.regist.showSendYZM = true;
							that.regist.time = 180;
						}
					}, 1000);
				});
			},
			//注册
			register(rouFrom) {
				this.$refs[rouFrom].validate((valid) => {
					if (valid) {
						var data = {
							username: this.regist.username,
							password: this.regist.password,
							email: this.regist.email
						}
						register(data, this.regist.code).then(res => {
							this.$user.setUserInfo(res.data);
							this.$store.commit("setUserInfo", res.data);
							var _this = this;
							this.$message({
								message: '注册成功！即将跳转博客',
								type: 'success',
								onClose() {
									var path = getPreView();
									//_this.$router.push(path ? path : '/');
									window.location.href = path ? path : '/';
								}
							});
						});
					}
				})
			}
		},
		created() {
			if (this.userInfo) {
				var path = getPreView();
				//this.$router.push(path ? path : '/');
				window.location.href = path ? path : '/';
			}
		},
		beforeCreate() {},
		mounted() {
			addJs("https://connect.qq.com/qc_jssdk.js");
			enter(this.submitLogin);
		}
	}